/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.notify.service;

import com.alibaba.fastjson.JSONObject;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.notify.dao.NotiUserDao;
import com.thinkgem.jeesite.modules.notify.entity.NotiUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 推送用户Service
 * @author chenbin
 * @version 2018-12-14
 */
@Service
@Transactional(readOnly = true)
public class NotiUserService extends CrudService<NotiUserDao, NotiUser> {

	@Autowired
	private NotiUserDao notiUserDao;

	public NotiUser get(String id) {
		return super.get(id);
	}
	
	public List<NotiUser> findList(NotiUser notiUser) {
		return super.findList(notiUser);
	}
	
	public Page<NotiUser> findPage(Page<NotiUser> page, NotiUser notiUser) {
		Page<NotiUser> page1 = super.findPage(page, notiUser);
		List<NotiUser> list = page1.getList();
		for (NotiUser notiUser1 : list) {
			String address = notiUser1.getAddress();
			if (!StringUtils.isEmpty(address)) {
				JSONObject jsonObject = JSONObject.parseObject(address);
				String data = jsonObject.getString("data");
//				System.out.println(data+"==========================");
				JSONObject dataJson = JSONObject.parseObject(data);
				String addressStr = dataJson.getString("area") + dataJson.getString("country") + dataJson.getString("city") + dataJson.getString("isp") + dataJson.getString("county");
				notiUser1.setAddress(addressStr);
			}
		}

		return page1;
	}
	
	@Transactional(readOnly = false)
	public void save(NotiUser notiUser) {
		super.save(notiUser);
	}
	
	@Transactional(readOnly = false)
	public void delete(NotiUser notiUser) {
		super.delete(notiUser);
	}

	public List<NotiUser> getUserByAliasOrTag(String tagOrAlias, String pushType) {
		return notiUserDao.getUserByAliasOrTag(tagOrAlias, pushType);
	}

    public NotiUser getUserByUsernameAndAppid(String username, String appid) {
        return notiUserDao.getUserByUsernameAndAppid(username, appid);
    }

	@Transactional(readOnly = false)
	public void updatUserByUsernameAndAppid(String appedition, String mac, String unitmodel, String imsi, String deviceId, String address, String ip, String userId) {
		NotiUser notiUser = new NotiUser();
		notiUser.setId(userId);
		notiUser.setAppEdition(appedition);
		notiUser.setMac(mac);
		notiUser.setUnitModel(unitmodel);
		notiUser.setImsi(imsi);
		notiUser.setDeviceId(deviceId);
		notiUser.setAddress(address);
		notiUser.setIp(ip);
		notiUserDao.updatUser(notiUser);
	}

}